package com.turtle.security.organizates.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.turtle.security.common.annotation.OperateLog;
import com.turtle.security.common.utils.ApiResult;
import com.turtle.security.common.utils.CustomerStringUtils;
import com.turtle.security.organizates.constant.SysRoleConstant;
import com.turtle.security.organizates.dto.SysRoleDTO;
import com.turtle.security.organizates.dto.SysRoleSearchDTO;
import com.turtle.security.organizates.entity.SysRole;
import com.turtle.security.organizates.service.impl.SysRoleServiceImpl;
import com.turtle.security.system.enums.OperateClient;
import com.turtle.security.system.enums.OperateType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统角色控制器
 *
 * @author turtle
 * date： 2020-05-29
 */
@RestController
@Api(value = "系统角色模块", tags = "系统角色模块")
@RequestMapping("/organizates/role")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))

public class SysRoleController {
    private final SysRoleServiceImpl sysRoleService;

    /**
     * 添加客户角色
     *
     * @param sysRole 角色信息
     * @return ApiResult
     */
    @PostMapping("add")
    @ApiOperation(value = "添加客户角色")
    @OperateLog(operateClient = OperateClient.MANAGE, module = "角色模块", operateType = OperateType.INSERT, description = "新增客户角色")
    public ApiResult roleAdd(@Validated @RequestBody SysRoleDTO sysRole) {
        // todo 获取当前登录用户信息
        sysRole.setCustomerId(1);
        sysRoleService.roleAdd(sysRole, SysRoleConstant.CUSTOMER_ROLE_TYPE);
        return ApiResult.success("添加客户角色成功");
    }

    /**
     * 更新系统角色
     *
     * @param sysRole 角色信息
     * @return ApiResult
     */
    @PutMapping("update")
    @ApiOperation(value = "更新系统角色")
    @OperateLog(operateClient = OperateClient.MANAGE, module = "角色模块", operateType = OperateType.UPDATE, description = "更新角色")
    public ApiResult roleUpdate(@Validated @RequestBody SysRoleDTO sysRole) {

        SysRole role = sysRoleService.getById(sysRole.getRoleId());
        if (CustomerStringUtils.isNull(role)) {
            return ApiResult.error("该角色不存在");
        }
        sysRoleService.roleUpdate(sysRole);
        return ApiResult.success("更新系统角色成功");
    }

    /**
     * 删除系统角色
     *
     * @param roleId 角色id
     * @return ApiResult
     */
    @DeleteMapping("delete/{roleId}")
    @ApiOperation(value = "删除系统角色")
    @OperateLog(operateClient = OperateClient.MANAGE, module = "角色模块", operateType = OperateType.DELETE, description = "删除角色")
    public ApiResult roleDelete(@Validated @PathVariable Long roleId) {
        SysRole role = sysRoleService.getById(roleId);
        if (CustomerStringUtils.isNull(role)) {
            return ApiResult.error("该角色不存在");
        }
        sysRoleService.roleDelete(roleId);

        return ApiResult.success("角色删除成功");
    }

    /**
     * 获取非系统类的角色列表-分页
     *
     * @param page    分页
     * @param sysRole 要查询的角色信息
     * @return ApiResult
     */
    @GetMapping("customer/list")
    @ApiOperation(value = "获取客户角色列表-分页")
    @OperateLog(operateClient = OperateClient.MANAGE, module = "角色模块", operateType = OperateType.LIST, description = "获取非系统类的角色列表-分页")
    public ApiResult customerRoleList(Page page, SysRoleSearchDTO sysRole) {
        Page<SysRole> roleList = sysRoleService.customerRoleList(page, sysRole);
        return ApiResult.success(roleList);
    }

}